package cn.sunxinao.pm.text;

import cn.sunxinao.pm.models.AbstractPet;

public class Pet extends AbstractPet {
    // 表格列数
    private static final int COLUMN = 8;
    // 分隔符
    private static final String SPLIT_STR = ",\t";

    public Pet() {
    }

    public static Pet ofString(String str) throws NumberFormatException {
        String[] data = str.split(",\\s*");
        if (data.length < COLUMN - 1) throw new IllegalArgumentException("Wrong student info string");
        long id = Long.parseLong(data[0].trim());
        String name = data[1].trim();
        boolean sex = Integer.parseInt(data[2].trim()) == 1;
        float age = Float.parseFloat(data[3].trim());
        String type = data[4].trim();
        String color = data[5].trim();
        String joinDate = data[6].trim();
        String owner = data.length > 7 ? data[7].trim() : "";
        Pet pet = new Pet();
        pet.id = id;
        pet.name = name;
        pet.sex = sex;
        pet.age = age;
        pet.type = type;
        pet.color = color;
        pet.joinDate = joinDate;
        pet.owner = owner;
        return pet;
    }

    public String writeToString() {
        return id + SPLIT_STR +
            name + SPLIT_STR +
            (sex ? "1" : "0") + SPLIT_STR +
            age + SPLIT_STR +
            type + SPLIT_STR +
            color + SPLIT_STR +
            joinDate + SPLIT_STR +
            owner;
    }
}
